package com.prig.base.repository;

import com.prig.base.entity.Permission;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

@Repository
public interface PermissionRepository extends JpaRepository<Permission,Long> {

    Page<Permission> findByNameLike(String keyword, Pageable pageable);

    @Query(nativeQuery = true,value = "SELECT\n" +
            "\tpermission.id,\n" +
            "\tpermission.icon,\n" +
            "\tpermission.`name`,\n" +
            "\tpermission.parent_id,\n" +
            "\tpermission.parent_ids,\n" +
            "\tpermission.sort_rank,\n" +
            "\tpermission.url,\n" +
            "\tpermission.is_view\n" +
            "FROM\n" +
            "\tpermission\n" +
            "INNER JOIN role_permission_relation ON permission.id = role_permission_relation.permission_id\n" +
            "WHERE\n" +
            "\trole_permission_relation.role_id in :roleIds")
    List<Permission> findByRoleIds(List<Long> roleIds);
    @Query(nativeQuery = true,value = "SELECT\n" +
            "\tpermission.id,\n" +
            "\tpermission.icon,\n" +
            "\tpermission.`name`,\n" +
            "\tpermission.parent_id,\n" +
            "\tpermission.parent_ids,\n" +
            "\tpermission.sort_rank,\n" +
            "\tpermission.url,\n" +
            "\tpermission.is_view\n" +
            "FROM\n" +
            "\tpermission\n" +
            "INNER JOIN role_permission_relation ON permission.id = role_permission_relation.permission_id\n" +
            "WHERE\n" +
            "\trole_permission_relation.role_id in :roleIds" +
            " AND permission.`name` like :keyword")
    Page<Permission> findByRoleIds(List<Long> roleIds,String keyword,Pageable pageable);
    @Query(nativeQuery = true,value = "SELECT\n" +
            "\tpermission.id,\n" +
            "\tpermission.icon,\n" +
            "\tpermission.`name`,\n" +
            "\tpermission.parent_id,\n" +
            "\tpermission.parent_ids,\n" +
            "\tpermission.sort_rank,\n" +
            "\tpermission.url,\n" +
            "\tpermission.is_view\n" +
            "FROM\n" +
            "\tpermission\n" +
            "INNER JOIN role_permission_relation ON permission.id = role_permission_relation.permission_id\n" +
            "WHERE\n" +
            "\trole_permission_relation.role_id in :roleIds")
    Page<Permission> findByRoleIds(List<Long> roleIds,Pageable pageable);
    @Query(nativeQuery = true,value = "SELECT\n" +
            "\tpermission.id,\n" +
            "\tpermission.icon,\n" +
            "\tpermission.`name`,\n" +
            "\tpermission.parent_id,\n" +
            "\tpermission.parent_ids,\n" +
            "\tpermission.sort_rank,\n" +
            "\tpermission.url,\n" +
            "\tpermission.is_view\n" +
            "FROM\n" +
            "\tpermission\n" +
            "INNER JOIN role_permission_relation ON permission.id = role_permission_relation.permission_id\n" +
            "INNER JOIN admin_role_relation ON role_permission_relation.role_id = admin_role_relation.role_id\n" +
            "WHERE\n" +
            "\tadmin_role_relation.admin_id = :adminId\n" +
            "AND permission.is_view = 1\n" +
            "ORDER BY\n" +
            "\tpermission.parent_id DESC")
    List<Permission> getMenu(long adminId);
    List<Permission> findByIsViewIsTrue();

    @Transactional
    @Query(nativeQuery = true,value = "delete from permission where id in :idsLong")
    @Modifying
    void deleteByIds(List<Long> idsLong);


}
